package com.example.dangerouscargodriver.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MultiListenerEditText extends androidx.appcompat.widget.AppCompatEditText{
    private List<OnEditorActionListener> mEditorActionListener = null;

    public interface OnEditorActionListener{
        void onEditorAction(TextView v, int actionId, KeyEvent event);
    }

    public MultiListenerEditText(Context context) {
        super(context);
        setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    for(int index = 0; index < mEditorActionListener.size(); index++){
                        mEditorActionListener.get(index).onEditorAction(v, actionId, event);
                    }
                    return true;
                }
                return false;
            }
        });
        mEditorActionListener = new ArrayList<>();
    }

    public MultiListenerEditText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    for(int index = 0; index < mEditorActionListener.size(); index++){
                        mEditorActionListener.get(index).onEditorAction(v, actionId, event);
                    }
                    return true;
                }
                return false;
            }
        });
        mEditorActionListener = new ArrayList<>();
    }

    public MultiListenerEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    for(int index = 0; index < mEditorActionListener.size(); index++){
                        mEditorActionListener.get(index).onEditorAction(v, actionId, event);
                    }
                    return true;
                }
                return false;
            }
        });
        mEditorActionListener = new ArrayList<>();
    }

    public void addEditorActionListener(OnEditorActionListener listener){
        for(int index = 0; index < mEditorActionListener.size(); index++){
            if(mEditorActionListener.get(index) == listener){
                return;
            }
        }

        mEditorActionListener.add(listener);
    }

    public void removeEditorActionListener(OnEditorActionListener listener){
        mEditorActionListener.remove(listener);
    }
}
